package com.globagames.isometric.core.view.actions
{
	import com.globagames.isometric.core.model.IsoEngineModelLocator;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.core.Application;
	import mx.core.UIComponent;
	import mx.managers.CursorManager;

	[Event(name="scrolled")]		
	public class AutoScrollAction extends UIComponent implements IAction {
		private var model:IsoEngineModelLocator = IsoEngineModelLocator.getInstance();
	
		public var newCursorID:Number = 0;
		
		private var dX:int = 1;
		private var dY:int = 1;
		private var marge:int = 16;
		private var velocity:int = 8;
		private var lastX:int;
		private var lastY:int;			
		private var timerStarted:Boolean;		
		private var forceMove :Boolean = false;
	
		
		public function AutoScrollAction() {
						
		}
		
		override protected function createChildren():void{
			super.createChildren();	
			this.width = model.screenWidth;
			this.height = model.screenHeight;
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {			
			super.updateDisplayList(unscaledWidth, unscaledHeight);				
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			invalidateDisplayList();			
		}

        public function rollOverHandler(event:Event):void {        	
	   		trace("rollOverHandler");	   			   		
	   		model.isoMap.myTimer.addEventListener("timer", renderHandler);	
        }
        public function rollOutHandler(event:Event):void {        	
	   		trace("rollOutHandler");
	   		model.isoMap.myTimer.removeEventListener("timer", renderHandler);
	   		if( CursorManager.currentCursorID == model.handCursorID ) {	
				CursorManager.removeCursor(CursorManager.currentCursorID);
			}	
			model.autoScrollingIsOn = false;
        }
        public function renderHandler(event:TimerEvent):void { 
        	if( model.draggingMapIsOn == false && model.isoMap.regX == model.isoMap.regNewX && model.isoMap.regY == model.isoMap.regNewY ) {
	        	var xx:int = stage.mouseX;
				var yy:int = stage.mouseY;
				dX = (xx-Math.min(Application.application.width,model.screenWidth)/2)/50;
				dY = (yy-Math.min(Application.application.height,model.screenHeight)/2)/50;
				if( dX > velocity) dX = velocity;
				if( dX < -velocity) dX = -velocity;
				if( dY > velocity) dY = velocity;
				if( dY < -velocity) dY = -velocity;
				if(  model.isoMap.mouseX > Math.min(Application.application.width -marge,model.screenWidth-marge) || model.isoMap.mouseX < marge ||  model.isoMap.mouseY < marge ||  model.isoMap.mouseY > Math.min(Application.application.height-marge,model.screenHeight-marge) ) {
					model.autoScrollingIsOn = true;
					if( CursorManager.currentCursorID == model.cursorID ) {	
						CursorManager.setCursor(model.dragCursorSymbol);
						model.handCursorID = CursorManager.currentCursorID;
					}
					model.isoMap.regX = model.screenWidth/2;
					model.isoMap.regNewX = model.isoMap.regX - dX;
					model.isoMap.regY = model.screenHeight/2;
					model.isoMap.regNewY = model.isoMap.regY - dY;
					model.isoMap.invalidateProperties();					
					event.updateAfterEvent();	
					var e:MouseEvent = new MouseEvent(MouseEvent.MOUSE_MOVE);
					e.localX = 10;
					e.localY = 10;
					model.isoMap.dispatchEvent(e);
																										
				} else {					
					if( CursorManager.currentCursorID == model.handCursorID ) {	
						CursorManager.removeCursor(CursorManager.currentCursorID);
					}
					model.autoScrollingIsOn = false; 
					model.isoMap.doRetrace = true;
					model.isoMap.invalidateProperties();					
					event.updateAfterEvent();
					
				}	
        	} 				
        }
		public function attach():void {
			systemManager.addEventListener(MouseEvent.ROLL_OVER , rollOverHandler);
			systemManager.addEventListener(MouseEvent.ROLL_OUT , rollOutHandler);
			model.isoMap.myTimer = new Timer(100 / stage.frameRate, 0);
			model.isoMap.myTimer.addEventListener("timer", renderHandler);				
			model.isoMap.myTimer.start();
			model.isoMap.regX = 0;
			model.isoMap.regY = 0;
			model.isoMap.regNewX = 0;
			model.isoMap.regNewY = 0;
			model.isoMap.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			
		}
		public function mouseMoveHandler(event:MouseEvent):void {
			event.updateAfterEvent();
		}
		
		public function detach():void {		
			model.isoMap.myTimer.stop();
			model.isoMap.myTimer.removeEventListener("timer", renderHandler);	
			systemManager.removeEventListener(MouseEvent.ROLL_OVER , rollOverHandler);
			systemManager.removeEventListener(MouseEvent.ROLL_OUT , rollOutHandler);
		}
	}
}